/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbcgateway.smscbresponse;

import com.elcom.emocbc.dbmapping.tables.pojos.SmscbMessageResult;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author GiangLT
 */
public class GetMessageInfoResponseHandler extends DefaultHandler {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private String temp;
    private SmscbMessageResult smscbMessageResult = new SmscbMessageResult();

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.S";
        DateFormat df = new SimpleDateFormat(DATE_FORMAT);
        if (qName.equals("startTime")) {
            try {
                smscbMessageResult.setStartTime(new Timestamp(df.parse(temp).getTime()));
            } catch (ParseException ex) {
                logger.error("Parse GetMessageInfoResponse Error: Wrong format. ", ex);
            }
        }
        if (qName.equals("endTime")) {
            try {
                smscbMessageResult.setEndTime(new Timestamp(df.parse(temp).getTime()));
            } catch (ParseException ex) {
                logger.error("Parse GetMessageInfoResponse Error: Wrong format. ", ex);
            }
        }
        if (qName.equals("state")) {
            smscbMessageResult.setBroadcastState(temp);
        }
        
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        temp = new String(ch, start, length);
    }

    public SmscbMessageResult getSmscbMessageResult() {
        return smscbMessageResult;
    }

    public void setSmscbMessageResult(SmscbMessageResult smscbMessageResult) {
        this.smscbMessageResult = smscbMessageResult;
    }
}
